#include<iostream>

using namespace std;

const int N = 1e5 + 10 , M = 1e6 + 10;
int q;
int op , x , y;
int id , ne[N] , e[N], mp[M];
int main()
{
    cin >> q;
    id++;
    e[id] = 1;
    mp[1] = id;
    while(q--)
    {
        cin >> op >> x;
        int p = mp[x];
        if(op == 1)
        {
            cin >> y;

            id++;
            e[id] = y;
            ne[id] = ne[p];
            ne[p] = id;
            mp[y] = id;
        }
        else if(op == 2)
        {
            cout << e[ne[p]] << endl;
        }
        else
        {
            ne[p] = ne[ne[p]];
        }
    }
    return 0;
}